package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Feedback;
import com.xuzhijie.pms.entity.Pet;
import com.xuzhijie.pms.entity.Property;
import com.xuzhijie.pms.entity.Renovation;
import com.xuzhijie.pms.entity.vo.PetVo;
import com.xuzhijie.pms.entity.vo.RenovationVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.PropertyMapper;
import com.xuzhijie.pms.mapper.RenovationMapper;
import com.xuzhijie.pms.service.IRenovationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@Service
public class RenovationServiceImpl extends ServiceImpl<RenovationMapper, Renovation> implements IRenovationService {

    @Resource
    private PropertyMapper propertyMapper;

    @Override
    public Result findPage(Integer page, Integer size, Integer currentProgress, Integer isReturn) {
        Page<Renovation> RenovationPage = new Page<>(page,size);
        QueryWrapper<Renovation> queryWrapper = new QueryWrapper<>();
        Page<Renovation> result;
        Long total;

        if (!currentProgress.equals(0)){
            queryWrapper.eq("current_progress",currentProgress);
        }

        if (!isReturn.equals(0)){
            queryWrapper.eq("is_return",isReturn);
        }


        try {
            result = page(RenovationPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }

        List<Renovation> RenovationList = result.getRecords();
        List<RenovationVo> voList = new ArrayList<>();
        for (Renovation renovation : RenovationList) {
            RenovationVo renovationVo = new RenovationVo(renovation);
            BeanUtil.copyProperties(renovation,renovationVo);
            voList.add(renovationVo);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",voList);
        resultMap.put("total",total);

        return Result.success(resultMap);
    }

    @Override
    public Result createSendReno(String[] ids, String id) {
        ArrayList<Renovation> renovations = new ArrayList<>();
        for (String s : ids) {
            Property property = propertyMapper.selectById(s);
            property.setRenovationStatus(1);
            propertyMapper.updateById(property);

            Renovation renovation = new Renovation();
            renovation.setCurrentProgress(1);
            renovation.setApplicant(id);
            renovation.setBuilding(s);
            renovation.setIsReturn(0);
            renovations.add(renovation);
        }
        saveBatch(renovations);

        return Result.success();
    }
}
